<template>
        <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="200px" size="medium">
            <el-form-item label="主机厂名称" prop="name">
                <el-input type="text" v-model="ruleForm.name" class="w300"></el-input>
            </el-form-item>
            <el-form-item label="主机厂状态" prop="status">
                <el-select v-model="ruleForm.status">
                    <el-option v-for="item in cOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="合作保司" prop="twList" v-if="flag === true">
                <el-checkbox-group v-model="ruleForm.twList" class="w180">
                    <el-checkbox v-for="item in cCheckList" :key="item" :label="item" disabled>{{item}}</el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <div v-else>
                <el-form-item label="管理员" prop="userName">
                    <el-input type="text" v-model="ruleForm.userName" class="w300" readonly></el-input>
                </el-form-item>
                <el-form-item label="管理员账号" prop="userAccount">
                    <el-input type="text" v-model="ruleForm.userAccount" class="w300" readonly></el-input>
                </el-form-item>
            </div>
            <div class="mb20 pdl200">注：初始密码为666666</div>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('ruleForm')">确 定</el-button>
                <el-button @click="onCancel()">取 消</el-button>
            </el-form-item>
        </el-form>
</template>

<script>
	export default {
        name: 'editCarFactory',
		data () {
			return {
                cOptions: [
                    { value: '在册', label: '在册' },
                    { value: '注销', label: '注销' }
                ],
                flag : false,
                cCheckList: ['阳光保险','太平保险'],
                ruleForm: {
                    index: 0,
                    name: '',
                    status: '',
                    twList: ['阳光保险','太平保险'],
                    userName: '',
                    userAccount: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请输入主机厂名称', trigger: 'blur' }
                    ],
                    status: [
                        { required: true, message: '请选择主机厂状态', trigger: 'change' }
                    ],
                    userName: [
                        { required: true, message: '请输入管理员名称', trigger: 'blur' }
                    ],
                    userAccount: [
                        { required: true, message: '请输入管理员账号', trigger: 'blur' }
                    ]
                }
            }
		},
		created () {
            if (this.$state.query && this.$state.query.carFactoryData) {
                this.flag = true,
                this.ruleForm = {
                    ...this.$state.query.carFactoryData
                }
            }
            this.ruleForm.userName = "",
            this.ruleForm.userAccount = ""
		},
        mounted() { },
		methods: {
            onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let lsData = JSON.parse(localStorage.getItem('carFactoryData'));
                        if(this.flag){
                            lsData[this.ruleForm.index - 1].name = this.ruleForm.name;
                            lsData[this.ruleForm.index - 1].status = this.ruleForm.status;
                        }else{
                            this.ruleForm.total = 0;
                            this.ruleForm.bxnums = 0;
                            this.ruleForm.index = lsData.length + 1;
                            lsData.push(this.ruleForm);
                        }
                        localStorage.setItem('carFactoryData', JSON.stringify(lsData));
                        this.$message.success('修改成功!')
                        this.$state.query.callback()
                        this.$modal.close()
                    } else {
                        return false;
                    }
                });
            },
            onCancel () {
                this.$modal.close();
            }
        }
	}
</script>

<style scoped>
    .w180{
        width: 180px;
    }
    .w300{
        width: 300px;
    }
</style>
